package com.tsf.demo.tools.controller;

import com.tsf.demo.commons.vo.Product;
import com.tsf.demo.tools.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

//tangfuzhong
@RestController
public class ProductController {

	@Autowired
	private ProductService productService;
	@Autowired
	private DiscoveryClient discoveryClient;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	//查询产品
	@RequestMapping(value = "/product/{id}", method = RequestMethod.GET)
	public Product getProduct(@PathVariable String id) {
		return productService.getProduct(id);
	}

	//查询产品
	@RequestMapping(value = "/product2/{id1}/{id2}/{id3}", method = RequestMethod.GET)
	public List<Product> getProduct2(@PathVariable String id1,@PathVariable String id2,@PathVariable String id3) {
		List<Product> list = new ArrayList<>();
		list.add(productService.getProduct(id1));
		list.add(productService.getProduct(id2));
		list.add(productService.getProduct(id3));
		return list;
	}

	@RequestMapping(value = "/product3/{id1}/{id2}/china/{id3}", method = RequestMethod.GET)
	public List<Product> getProduct3(@PathVariable String id1,@PathVariable String id2,@PathVariable String id3) {
		List<Product> list = new ArrayList<>();
		list.add(productService.getProduct(id1));
		list.add(productService.getProduct(id2));
		list.add(productService.getProduct(id3));
		return list;
	}

	//查询产品
	@RequestMapping(value = "/product/list", method = RequestMethod.GET)
	public List<Product> getProductList() {
		return productService.getList();
	}

	//入库
	@RequestMapping(value = "/inventory/inbound", method = RequestMethod.GET)
	public String inbound(@RequestParam(value="productId", required=true) String productId,
						  @RequestParam(value="quantity", required=true) Integer quantity) {
		return productService.inbound(productId,quantity);
	}

	//出库
	@RequestMapping(value = "/inventory/outbound", method = RequestMethod.GET)
	public String outbound(@RequestParam(value="productId", required=true) String productId,
						  @RequestParam(value="quantity", required=true) Integer quantity) {
		return productService.outbound(productId,quantity);
	}



	//尝试查询当前实例的服务列表
	@RequestMapping(value = "/echo-serverList", method = RequestMethod.GET)
	public List<Object> getServerList() {
		return getServerList(discoveryClient);
	}

	public static List<Object> getServerList(DiscoveryClient discoveryClient) {
		List<Object> listAll = new ArrayList<Object>();
		List<String> list = discoveryClient.getServices();
		listAll.add(list);//显示所有serviceId
		for (String serviceId:list) {
			List<ServiceInstance> listService = discoveryClient.getInstances(serviceId);
			listAll.addAll(listService);
		}

		String discoveryClientInfo = "discoveryClient=" + discoveryClient.getClass().getName();
		listAll.add(discoveryClientInfo);
		return listAll;
	}

}